package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.millioncall.openplatform.common.PageNoSizePairImpl;
import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.OrderDAO;
import org.millioncall.yueyoga.admin.model.Order;
import org.millioncall.yueyoga.admin.model.TinyOrderModel;
import org.springframework.stereotype.Repository;

@Repository
public class OrderDAOImpl extends BaseDao implements OrderDAO {

	@Override
	public Order findById(Integer id) {
		return this.getPagableSqlSessionTemplate().selectOne("findOrderById",
				id);
	}

	@Override
	public PaginatedList<Order> findInPage(int start, int limit) {
		PreparedQuery<Order> pq = this.getPagableSqlSessionTemplate().findAll(
				"findAllOrder");
		PaginatedList<Order> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public int save(Order order) {
		return this.getPagableSqlSessionTemplate().insert("saveOrder", order);
	}

	@Override
	public int deleteByIds(String[] ids) {
		return 0;
	}

	@Override
	public PaginatedList<Order> findAll() {
		return null;
	}

	@Override
	public Order findByOrder(Order order) {
		Order result = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", order.getMember().getMemberid());
		map.put("money", order.getMoney());
		map.put("score", order.getScore());
		map.put("remarks", order.getRemarks());
		map.put("inputTime", order.getInputTime());
		result = this.getPagableSqlSessionTemplate().selectOne(
				"selectOrderOne", map);
		return result;
	}

	@Override
	public PaginatedList<Order> findByMemberId(Integer pageNo, Integer limit,
			Integer memberId) {
		PreparedQuery<Order> pq = this.getPagableSqlSessionTemplate().findAll(
				"findOrderByMemberId", memberId);
		PaginatedList<Order> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(pageNo, limit)));
		return pl;
	}

	@Override
	public PaginatedList<Order> findOrdersByCdts(int pageNo, int pageSize,
			Map<String, Object> map) {
		PreparedQuery<Order> pq = this.getPagableSqlSessionTemplate().findAll(
				"findOrdersByCdts", map);
		PaginatedList<Order> pl = pq.list(new PaginateParams(
				new PageNoSizePairImpl(pageNo, pageSize)));
		return pl;
	}

	@Override
	public PaginatedList<Order> findGoodsOrdersByCdts(int start, int limit,
			Map<String, Object> map) {
		PreparedQuery<Order> pq = this.getPagableSqlSessionTemplate().findAll(
				"findGoodsOrdersByCdts", map);
		PaginatedList<Order> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public PaginatedList<TinyOrderModel> findAllBuyRecord(int pageNo,
			int pageSize, Map<String, Object> params) {
		PreparedQuery<TinyOrderModel> pq = this.getPagableSqlSessionTemplate()
				.findAll("findAllOrderTiny", params);
		PaginatedList<TinyOrderModel> pl = pq.list(new PaginateParams(
				new PageNoSizePairImpl(pageNo, pageSize)));
		return pl;
	}

	@Override
	public int disableOrder(String[] Ids) {
		Integer Id = 0;
		for(String strId : Ids) {
			try {
				Id = Integer.parseInt(strId);
				disableOrder(Id);
			} catch (NumberFormatException e) {
				return 0;
			}
		}
		return 1;
	}
	
	public int disableOrder(Integer Id) {
		return this.getPagableSqlSessionTemplate().update("disableOrder", Id);
	}

	@Override
	public int getOrderEnabledCountByMemberId(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectOne("getOrderEnabledCountByMemberId", map);
	}

	@Override
	public int updateOrderType(Order order) {
		return this.getPagableSqlSessionTemplate().update("updateOrderType", order);
	}

	@Override
	public int disableOrder(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().update("disableOrderByMap", map);
	}
}
